package com.wn.quality.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.net.UnknownHostException;

/**
 * @author wuqingting
 * @date 2022/10/20
 */
@Configuration
public class RedisConfiguration {
    @Bean
//本代码代替RedisAutoConfiguration的redisTemplate,此次进行了配置生成bean,则RedisAutoConfiguration的redisTemplate不会生成bean(存在@ConditionalOnMissingBean)
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //修改默认序列化器
        //template.setDefaultSerializer(RedisSerializer.json());

        //分别设置序列化格式
        template.setKeySerializer(RedisSerializer.string());//设置字符串数据类型的键类型为string
        template.setValueSerializer(RedisSerializer.json());//设置字符串数据类型的键类型为json
        template.setHashKeySerializer(RedisSerializer.string());//设置hash数据类型的键类型为string
        template.setHashValueSerializer(RedisSerializer.json());//设置hash数据类型的键类型为json

        return template;
    }
}
